package com.mb.app.appused.views;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.mb.app.appused.R;

public class GroupSelectorLayout extends ScrollView {

	Context context;
	LinearLayout layout;

	public GroupSelectorLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public GroupSelectorLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public GroupSelectorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		init(context);
	}

	void init(Context context) {
		this.context = context;
		layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setLayoutParams(getFallWidth());
		this.addView(layout);
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
		addChild();
	}

	public void addChild() {
		View item = new ItemView(context);
		item.setLayoutParams(getFallWidth(100));
		layout.addView(item);
	}

	public void addChild(int icon, String lable, String hit) {
		FrameLayout child = new FrameLayout(this.context);
		child.setBackgroundColor(Color.parseColor("#F5F5DC"));
	}

	LayoutParams getFallWidth(int height) {
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, height);
		return lp;
	}

	LayoutParams getFallWidth() {
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		return lp;
	}

	/**
	 * 绘制子对象
	 * 
	 * @author xhwwzd
	 *
	 */
	public class ItemView extends View {
		private String mTitleText = "子对象";

		private Paint bgPaint;
		private Paint linePaint;
		private Paint iconpaint;
		private Paint txtpaint;

		private int iconSize = 10;
		private int arrowSize = 10;
		private float txtSize = 20;

		public int icon = R.drawable.ic_launcher;
		public int arrow = R.drawable.ic_launcher;

		Bitmap bitMap;
		Bitmap arrowMap;

		int bacground = Color.parseColor("#ff0000");
		int lineColor = Color.parseColor("#cccccc");
		int txtColor = Color.parseColor("#ffffff");

		Rect txtBound;

		public ItemView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			init(context);
		}

		void init(Context context) {

			bgPaint = new Paint();
			bgPaint.setAntiAlias(true);
			bgPaint.setColor(bacground);
			bgPaint.setStyle(Paint.Style.FILL);

			linePaint = new Paint();
			linePaint.setAntiAlias(true);
			linePaint.setColor(lineColor);
			linePaint.setStyle(Paint.Style.FILL);

			iconpaint = new Paint();

			bitMap = ((BitmapDrawable) context.getResources().getDrawable(icon)).getBitmap();
			arrowMap = ((BitmapDrawable) context.getResources().getDrawable(arrow)).getBitmap();

			txtpaint = new Paint();
			txtpaint.setTextSize(txtSize);
			txtpaint.setColor(txtColor);

			txtBound = new Rect();
			txtpaint.getTextBounds(mTitleText, 0, mTitleText.length(), txtBound);
		}

		@SuppressLint("DrawAllocation")
		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			super.onDraw(canvas);
			canvas.drawRect(0, 0, getWidth(), 1, linePaint);
			canvas.drawRect(0, 1, getWidth(), getHeight() - 1, bgPaint);

			iconSize = (int) (getHeight() * 0.75f);
			arrowSize = (int) (getHeight() * 0.5f);

			Rect mDestRect = new Rect();
			mDestRect.left = 10;
			mDestRect.top = (getHeight() - iconSize) / 2;
			mDestRect.right = mDestRect.left + iconSize;
			mDestRect.bottom = mDestRect.top + iconSize;

			canvas.drawBitmap(bitMap, null, mDestRect, iconpaint);

			Rect arrowRect = new Rect();
			arrowRect.left = getWidth() - arrowSize - 10;
			arrowRect.top = (getHeight() - arrowSize) / 2;
			arrowRect.right = arrowRect.left + arrowSize;
			arrowRect.bottom = arrowRect.top + arrowSize;

			canvas.drawBitmap(arrowMap, null, arrowRect, iconpaint);

			canvas.drawRect(0, getHeight() - 1, getWidth(), getHeight(), linePaint);

			canvas.drawText(mTitleText, mDestRect.right +1 + txtBound.width() / 2,
					getHeight() / 2 + txtBound.height() / 2, txtpaint);
		}
	}
}
